package csc.daonjpa.java.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import net.tanesha.recaptcha.ReCaptchaResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import share.Constant;
import csc.daonjpa.java.domain.Admin;
import csc.daonjpa.java.domain.JSONResponse;
import csc.daonjpa.java.domain.Log;
import csc.daonjpa.java.domain.Transaction;
import csc.daonjpa.java.service.AdminService;
import csc.daonjpa.java.service.LogService;
import csc.daonjpa.java.service.TransactionService;
import csc.daonjpa.java.service.TransactionStateService;
import net.tanesha.recaptcha.ReCaptchaImpl;

@Controller
@RequestMapping(value = {"admin/"})
public class AdminController {
	
	@Autowired
	private AdminService adminService;
	
	@Autowired
	private TransactionService transactionService;
	
	@Autowired
	private TransactionStateService transactionStateService;
	
	@Autowired
	private LogService logservice;
	
	@Autowired
    ReCaptchaImpl reCaptcha;
	
	public AdminService getAdminService() {
		return adminService;
	}


	public void setAdminService(AdminService adminService) {
		this.adminService = adminService;
	}
	
	public TransactionService getTransactionService() {
		return transactionService;
	}


	public void setTransactionService(TransactionService transactionService) {
		this.transactionService = transactionService;
	}

	@RequestMapping(value = {"login"})
	public String toAdminLogin() {
		return Constant.VIEW_ADMIN_LOGIN;
	}
	
	@RequestMapping(value = {"loginAdmin"})
	public String doLoginAdmin(HttpServletRequest request, Model model,
			@RequestParam("recaptcha_challenge_field") String challengeField, 
	        @RequestParam("recaptcha_response_field") String responseField) {
		
		String loginId = request.getParameter(Constant.PARAM_LOGINID);
		String password = request.getParameter(Constant.PARAM_PASSWORD);
		
		String remoteAddress = request.getRemoteAddr();
        ReCaptchaResponse reCaptchaResponse = this.reCaptcha.checkAnswer(remoteAddress, challengeField, responseField);
		
		Admin admin = adminService.checkLogin(loginId, password);

			if (admin != null) {
				if(reCaptchaResponse.isValid()){
					request.getSession().setAttribute(
							Constant.SESSION_ATR_CUSTOMER_ID, admin.getAdminId());
					List<Transaction> lstTransaction = transactionService
							.getAllTransactionList();
					request.getSession().setAttribute(Constant.SESSION_ATR_IS_CUSTOMER, new Boolean(false));
					model.addAttribute(Constant.MODEL_OBJ_SENT_TRANSACTION, lstTransaction);
					return Constant.VIEW_ADMIN;
				}
				else{
					JSONResponse response = new JSONResponse();
					response.setSuccess(false);
					response.setMessage("Invalid captcha please try again!");
					model.addAttribute(Constant.MODEL_OBJ_LOGIN_RESULT, response);
					return Constant.VIEW_ADMIN_LOGIN;
				}
				
			} else {
				JSONResponse response = new JSONResponse();
				response.setMessage("Invalid username or password!");
				request.getSession().removeAttribute(
						Constant.SESSION_ATR_CUSTOMER_ID);
				model.addAttribute(Constant.MODEL_OBJ_LOGIN_RESULT, response);
				System.out.println("Khong phai admin");
				return Constant.VIEW_ADMIN_LOGIN;
			}
		} 
	
	@RequestMapping(value = {"transactionHistory"})
	public String toAdminTransactionHistory(Model model) {
		List<Transaction> lstTransaction = transactionService.getAllTransactionList();
		model.addAttribute(Constant.MODEL_OBJ_SENT_TRANSACTION, lstTransaction);
		return Constant.VIEW_ADMIN;
	}
	
	@RequestMapping(value={"viewTransactionDetail"})
	public String adminViewTransactionDetail(HttpServletRequest request, Model model){
		Long transactionId = Long.parseLong(request.getParameter(Constant.PARAM_TRANSACTION_ID));
		Transaction transaction = transactionService.getTransactionDetailById(transactionId);
		model.addAttribute(Constant.MODEL_OBJ_TRANSACTION_DETAIL, transaction);
		return Constant.VIEW_ADMIN_DETAIL;
	}
	
	@RequestMapping(value={"viewRequestApprove"})
	public String adminRequestApproved(HttpServletRequest request, Model model) {
		Long transactionId = Long.parseLong(request.getParameter("transactionId"));
		Transaction transaction = transactionService.getTransactionDetailById(transactionId);
		model.addAttribute(Constant.MODEL_OBJ_TRANSACTION_DETAIL, transaction);
		return Constant.VIEW_ADMIN_DETAIL_APPROVE;
	}
	
	@RequestMapping(value={"aproveTransaction"})
	public String aproveTransaction(HttpServletRequest request, Model model){
		Long transactionId = Long.parseLong(request.getParameter(Constant.PARAM_TRANSACTION_ID));
		transactionService.setApproveTransactionById(transactionId);
		toAdminTransactionHistory(model);
		return Constant.VIEW_ADMIN;
	}
	
	@RequestMapping(value={"denyTransaction"})
	public String denyTransaction(HttpServletRequest request, Model model){
		Long transactionId = Long.parseLong(request.getParameter(Constant.PARAM_TRANSACTION_ID));
		String reason = request.getParameter(Constant.PARAM_REASON);
		transactionService.setDenyTransactionById(transactionId, reason);
		toAdminTransactionHistory(model);
		return Constant.VIEW_ADMIN;
	}
	
	@RequestMapping(value={"logout"})
	public String logoutAdmin(Model model, HttpServletRequest request){
		request.getSession().removeAttribute(Constant.SESSION_ATR_CUSTOMER_ID);
		request.getSession().removeAttribute(Constant.SESSION_ATR_IS_CUSTOMER);
		request.getSession().removeAttribute(Constant.SESSION_ATR_LOGIN_FAIL);
		request.getSession().removeAttribute(Constant.SESSION_ATR_LOGINID);
		return Constant.VIEW_ADMIN_LOGIN;
	}
	
	@RequestMapping(value = {"log"})
	public String adminViewLog(Model model, HttpServletRequest request) {
		List<Log> logList = logservice.getLogs();
		model.addAttribute(Constant.MODEL_OBJ_LOG_LIST, logList);
		return Constant.VIEW_ADMIN_LOG;
	}
}
